<template>
  <!-- 上传附件弹窗 -->
  <el-dialog
    title="上传附件"
    width="640px"
    :visible.sync="showTaskProp"
    :close-on-click-modal="false"
    @close="handleClose"
    :modal-append-to-body="false"
  >
    <div class="diaTop">
      <el-form
        :model="formData"
        ref="formData"
        :rules="formRules"
        label-width="120px"
      >
        <el-form-item
          label="考勤日期"
          v-if="uploadType === 'SMALL_PROJECT_ATT'"
          prop="insDate"
        >
          <el-date-picker
            v-model="formData.insDate"
            type="date"
            placeholder="选择日期"
            clearable
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item
          label="发薪月份"
          v-if="uploadType === 'SMALL_PROJECT_SALARY'"
          prop="month"
        >
          <el-date-picker
            v-model="formData.month"
            type="month"
            placeholder="选择月份"
            clearable
            value-format="yyyy-MM"
          ></el-date-picker>
        </el-form-item>
        <el-form-item
          prop="upLoadFile"
          :label="titleTip"
          class="upLoadDomContainer"
        >
          <uploadAnnex
            @getFileList="getFileList"
            :limitationFactor="limitationFactorInsurance"
            @getFile="getFile"
            :upLoadFile="upLoadFile"
          />
        </el-form-item>
        <uploadResult
          style="margin-left: 120px"
          :resultList="upLoadFile"
          @callBackResult="getFile"
        />
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button class="cancel" @click="handleClose">取消</el-button>
      <el-button
        type="primary"
        class="save"
        :loading="isSaveLoading"
        @click="saveForm"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'uploadFileDialog',
  props: {
    showTaskProp: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: String,
    },
    uploadType: {
      type: String,
      default: 'SMALL_PROJECT_EMPLOYEE', ////花名册:SMALL_PROJECT_EMPLOYEE; 考勤:SMALL_PROJECT_ATT; 工资:SMALL_PROJECT_SALARY
    },
  },
  data() {
    var checkUpLoadFile = (rule, value, callback) => {
      if (this.upLoadFile.length == 0) {
        return callback(new Error('附件不能为空'))
      } else {
        callback()
      }
    }
    return {
      showTask: false,
      titleTip: '上传实名附件',
      formData: {
        insDate: '', //考勤日期
        month: '', //发薪月份
      },
      formRules: {
        insDate: [
          {
            required: true,
            message: '请选择考勤日期',
            trigger: 'change',
          },
        ],
        month: [
          {
            required: true,
            message: '请选择发薪月份',
            trigger: 'change',
          },
        ],
        upLoadFile: [
          {
            required: true,
            validator: checkUpLoadFile,
            trigger: 'blur',
          },
        ],
      },
      oldSrcList: [],
      srcList: [],
      upLoadFile: [],
      limitationFactorInsurance: {
        uploadAnnexUrl: 'api-f/files/upload', //自定义上传文件接口地址
        upLoadFileLength: 10, //自定义上传附件的个数设置
        fileSize: 5, //自定义上传文件最大设置
        formatType: 'typeFour', //自定义上传格式校验类型
        fileId: 'salaryExcelInsurance', //自定义id解决一个页面包含多个上传附件
        uploadFormId: 'salaryExcelInsuranceForm', //自定义id解决一个页面包含多个上传附件
        messagetip:
          '要求格式为png，jpg，jpeg，doc，docx，xls，xlsx，pdf格式，附件大小不超过5M', //自定义报错提示语
        methodTip:
          '附件格式支持png，jpg，jpeg，doc，docx，xls，xlsx，pdf，最多上传10个附件', //自定义上传文件提示
        uploadBtnType: 'typeOne', //自定义上传按钮类型
      },
    }
  },
  mounted() {
    this.titleTip =
      this.uploadType === 'SMALL_PROJECT_ATT'
        ? '上传考勤附件'
        : this.uploadType === 'SMALL_PROJECT_SALARY'
        ? '上传发薪附件'
        : '上传实名附件'
  },
  methods: {
    getFileList(val) {
      this.oldSrcList.push(val)
      this.srcList.push(val)
    },
    getFile(val) {
      this.upLoadFile = val
    },
    saveForm() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.axios({
            method: 'post',
            url: 'api-e/cs/employee/expand/saveMiniProjectFile',
            data: {
              projectId: this.projectId,
              type: this.uploadType,
              remark: this.formData.insDate || this.formData.month,
              files: JSON.stringify(this.upLoadFile),
            },
          })
            .then((res) => {
              if (!res.data.hasError) {
                this.$message.success('上传成功')
                this.$emit('handleBack')
              } else {
                this.$message.success('上传失败')
              }
            })
            .catch((error) => {
              this.$message.success('上传失败')
            })
        } else {
          this.$message.error('请按照提示规则填写')
        }
      })
    },
    handleClose() {
      this.upLoadFile = []
      this.$refs.formData.resetFields()
      this.$emit('handleBack')
    },
  },
}
</script>
<style lang="less" scoped>
.el-dialog__body {
  padding: 0 !important;
}
.diaTop {
  padding: 24px 30px;
  .tips {
    line-height: 18px;
    font-size: 14px;
    color: #333333;
    font-weight: 400;
    margin-bottom: 12px;
  }
  .el-row {
    border-top: 1px solid #dbdbdb;
    border-left: 1px solid #dbdbdb;
    .el-col {
      border-right: 1px solid #dbdbdb;
      border-bottom: 1px solid #dbdbdb;
      line-height: 40px;
      text-align: center;
      color: #333333;
      font-size: 14px;
      font-weight: 400;
    }
  }
}
.diaBot {
  position: relative;
  padding: 15px;
  &::after {
    position: absolute;
    right: 0px;
    top: 0;
    left: 0px;
    height: 1px;
    content: '';
    width: 100%;
    -webkit-transform: scaleY(0.5);
    transform: scaleY(0.5);
    background-color: #e3e7e9;
    z-index: 10;
  }
  .el-col {
    padding: 5px;
    .infoBox {
      background: rgba(216, 216, 255, 0.2);
      height: 110px;
      padding: 20px;
      div {
        line-height: 24px;
        height: 24px;
      }
      .iconfont {
        font-size: 24px;
        color: #265faa;
      }
      span {
        font-size: 18px;
        font-weight: 600;
        color: #000;
        padding-left: 10px;
      }
      p {
        line-height: 20px;
        margin-top: 10px;
        font-size: 14px;
        color: #666;
      }
    }
  }
}
.theme_cold .diaBot .infoBox .iconfont {
  color: #009588;
}
</style>
